﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Models;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using System.Data;

namespace WebHoiChoMuaBan.Controllers.Private
{
    public class FriendController : PrivateController
    {
        //
        // GET: /Friend/
        private void Init()
        {
            ViewData["mid"] = "friend";
            LoadTimeAndCurrency();
        }

        public ActionResult Index(int? uid)
        {
            if (uid == null)
                return HomePage();
            if (LoadMember(uid.Value) == null)
            {
                Init();
                return View("~/Views/Private/FriendEmailService.aspx");
            }
            return HomePage();
        }

        //
        // GET: /Friend/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Friend/MakeFriend

        //public ActionResult MakeFriend(int? id)
        //{
        //    if (id != null)
        //    {
        //        MemberHelper hMember = new MemberHelper(id.Value);
        //        Member member = hMember.Member;
        //        if (member != null)
        //        {
        //            int? myId = (int?)Session["MemId"];
        //            if (myId != null)
        //            {
        //                if (hMember.SendMakeFriendRequest(myId.Value))
        //                    Response.Write('R'); //Requested
        //                else
        //                    Response.Write('F'); // Request failed
        //            }
        //            else
        //                Response.Write('L'); //Not login yet
        //        }
        //        else
        //            Response.Write('E'); //Requested member not existed
        //    }
        //    else
        //        Response.Write('F'); // Request failed
        //    return null;
        //} 

        //
        // POST: /Friend/MakeFriend


        //
        // GET: /Friend/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Friend/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Friend/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Friend/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        public ActionResult EmailService()
        {
            return View("~/Views/Private/FriendEmailService.aspx");
        }

        public ActionResult MakeFriendService(int? uid)
        {
            if (uid == null)
                return HomePage();
            if (LoadMember(uid.Value) == null)
            {
                Init();
                return View("~/Views/Private/MakeFriend.aspx");
            }
            return HomePage();
        }

        [HttpPost]
        public string MakeFriend(string requestingId, string requestedId, char Decision)
        {
            bool isSuccess = new FriendHelper().SubmitMakeFriend(int.Parse(requestingId), int.Parse(requestedId), Decision);
            if (isSuccess)
            {
                List<Member> result = new FriendHelper().getAllPendingMembers(int.Parse(requestingId));
                DataTable dt = new DataTable();
                dt.Columns.Add("MemId");
                dt.Columns.Add("MemUsername");
                dt.Columns.Add("MemPassword");
                dt.Columns.Add("MemEmail");
                dt.Columns.Add("MemFullname");
                dt.Columns.Add("MemAvatar");
                Member mem = new Member();
                for (int i = 0; i < result.Count; i++)
                {
                    mem = result[i];
                    dt.Rows.Add(dt.NewRow());
                    dt.Rows[i]["MemId"] = mem.MemId;
                    dt.Rows[i]["MemUsername"] = mem.MemUsername;
                    dt.Rows[i]["MemPassword"] = mem.MemPassword;
                    dt.Rows[i]["MemEmail"] = mem.MemEmail;
                    dt.Rows[i]["MemFullname"] = mem.MemFullname;
                    dt.Rows[i]["MemAvatar"] = mem.MemAvatar;
                }

                //List<Member> result2 = new FriendHelper().getAllApprovedMembers(int.Parse(requestingId));
                //DataTable dt2 = new DataTable();
                //dt2.Columns.Add("MemId");
                //dt2.Columns.Add("MemUsername");
                //dt2.Columns.Add("MemPassword");
                //dt2.Columns.Add("MemEmail");
                //dt2.Columns.Add("MemFullname");
                //dt2.Columns.Add("MemAvatar");
                //Member mem2 = new Member();
                //for (int i = 0; i < result2.Count; i++)
                //{
                //    mem2 = result2[i];
                //    dt2.Rows.Add(dt2.NewRow());
                //    dt2.Rows[i]["MemId"] = mem2.MemId;
                //    dt2.Rows[i]["MemUsername"] = mem2.MemUsername;
                //    dt2.Rows[i]["MemPassword"] = mem2.MemPassword;
                //    dt2.Rows[i]["MemEmail"] = mem2.MemEmail;
                //    dt2.Rows[i]["MemFullname"] = mem2.MemFullname;
                //    dt2.Rows[i]["MemAvatar"] = mem2.MemAvatar;
                //}

                string json = JsonConvert.SerializeObject(dt);
                //string json2 = JsonConvert.SerializeObject(dt2);
                //string[] jsonresult = { json, json2 };
                return json;
            }
            else return null;
        }

        [HttpPost]
        public string MakeFriendUpdate(string requestingId)
        {
            List<Member> result2 = new FriendHelper().getAllApprovedMembers(int.Parse(requestingId));
            DataTable dt2 = new DataTable();
            dt2.Columns.Add("MemId");
            dt2.Columns.Add("MemUsername");
            dt2.Columns.Add("MemPassword");
            dt2.Columns.Add("MemEmail");
            dt2.Columns.Add("MemFullname");
            dt2.Columns.Add("MemAvatar");
            Member mem2 = new Member();
            for (int i = 0; i < result2.Count; i++)
            {
                mem2 = result2[i];
                dt2.Rows.Add(dt2.NewRow());
                dt2.Rows[i]["MemId"] = mem2.MemId;
                dt2.Rows[i]["MemUsername"] = mem2.MemUsername;
                dt2.Rows[i]["MemPassword"] = mem2.MemPassword;
                dt2.Rows[i]["MemEmail"] = mem2.MemEmail;
                dt2.Rows[i]["MemFullname"] = mem2.MemFullname;
                dt2.Rows[i]["MemAvatar"] = mem2.MemAvatar;
            }

            string json = JsonConvert.SerializeObject(dt2);
            return json;
        }

        [HttpPost]
        public string FriendRequest(string requestingId, string requestedId)
        {
            bool isSuccess = new FriendHelper().FriendRequest(int.Parse(requestingId), int.Parse(requestedId));
            if (isSuccess)
                return "true";
            else
                return "false";
        }
    }
}
